<template>
  <div class="execution">
    <basic-container>
      <avue-crud
        :data="data"
        :option="option"
        :search.sync="search"
        :table-loading="loading"
        @on-load="listdata"
        @refresh-change="listdata"
        @search-change="listdata"
        :page.sync="page"
        @row-update="rowUpdate"
        @row-del="rowDel"
        @selection-change="selectionChange"
      >
        <!-- 自定义按钮 -->
        <template slot-scope="scope" slot="menuLeft">
          <el-button
            icon="el-icon-plus"
            size="small"
            type="primary"
            plain
            @click="$refs.excelUpload.show()"
            >批量导入
          </el-button>
          <el-button
            icon="el-icon-plus"
            size="small"
            type="danger"
            plain
            @click="batchDelet"
            >批量删除
          </el-button>
        </template>
      </avue-crud>
      <!--excel 模板导入 -->
      <excel-upload
        ref="excelUpload"
        title="信息导入"
        url="/admin/order/importOrder"
        @refreshDataList="listdata"
      ></excel-upload>
    </basic-container>
  </div>
</template>

<script>
import ExcelUpload from "@/components/upload/excel";
import { option } from "@/const/crud/houxin/active";
import { del, queryOrderList, update } from "@/api/houxin/order";

export default {
  components: { ExcelUpload },
  data() {
    return {
      option: option,
      data: [],
      loading: true,
      search: {},
      selectsList: "",
      page: {
        total: 0, // 总页数
        currentPage: 1, // 当前页数
        pageSize: 20, // 每页显示多少条
      },
    };
  },
  methods: {
    batchDelet() {
      if (!this.selectsList) {
        this.$message({
          message: "未选择",
          type: "warning",
        });
        return;
      }
      let data = {
        orderIds: this.selectsList.map((e) => {
          return e.id;
        }),
      };

      this.$confirm("批量删除操作, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          del(data).then((r) => {
            this.listdata();
            this.$message({
              type: "success",
              message: "删除成功!",
            });
          });
        })
        .catch(() => {});
    },
    selectionChange(list) {
      this.selectsList = list;
    },
    rowUpdate(form, index, done, loading) {
      update(form).then(() => {
        this.listdata();
      });
      loading();
      done();
    },
    rowDel(row, index) {
      this.$confirm("删除操作, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          del({ orderIds: [row.id] }).then((r) => {
            this.listdata();
            this.$message({
              type: "success",
              message: "删除成功!",
            });
          });
        })
        .catch(() => {});
    },
    listdata(params, done) {
      if (done) {
        this.page.currentPage = 1;
      }
      let searchParams = {
        ...this.search,
        pageIndex: this.page.currentPage,
        pageSize: this.page.pageSize,
        menuType: 0,
      };
      searchParams.deviceNo = searchParams.deviceNo || null;
      searchParams.orderNo = searchParams.orderNo || null;

      this.loading = true;
      queryOrderList(searchParams).then((r) => {
        this.data = r.data.data.list;
        this.page.total = r.data.data.total;
        this.loading = false;
      });
      done && done();
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
  },
};
</script>

<style></style>
